# -- coding: utf-8 --

# @Author  : 游婷

# @File    : SendMethod.py

# @Project : python上课代码


import requests


class SendMethod(object):
    '''
    请求的公共方法
    '''

    @staticmethod  # 静态方法
    def send_method(method: str, url: str, params=None, data=None, json=None, headers=None):
        if method.upper() == 'GET':
            response = requests.get(url, params=params, headers=headers)
        elif method.upper() == 'POST':
            if data is not None:
                response = requests.post(url, data=data, headers=headers)
            elif json is not None:
                response = requests.post(url, json=json, headers=headers)
            else:
                response = requests.post(url, headers=headers)
        else:
            print('请求方式只支持get和post')
            return None
        result = {}
        result['status'] = response.status_code  # 状态码
        result['headers'] = response.headers  # 响应头
        # body里面有我们传入参数的数据,比如id,name等等'
        result['body'] = response.json()  # ------>response.json() 是一个字典形式,很像json字符串格式的字典
        result["response_time"] = response.elapsed.microseconds / 1000
        return result


if __name__ == '__main__':
    URL = "http://127.0.0.1:8000/api/get_event_list/"
    # 3.准备payload 字典的形式
    payload = {'eid': "2"}
    print(SendMethod.send_method(url=URL, method="get", params=payload))
